<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>EASY后台-新增</title>
    <link rel="icon" th:href="@{/static/images/favicon.ico}">
    <link rel="stylesheet" th:href="@{/static/css/common.css}">
    <link rel="stylesheet" th:href="@{/static/layui/css/layui.css}">
</head>
<body>
<div class="easy-container">
    <form class="layui-form" id="dataForm" lay-filter="dataForm">
        <div class="layui-row">
            <div class="layui-col-sm-3 layui-col-md3 layui-col-lg3">
                <div class="layui-form-item">
                    <label class="layui-form-label required">表名</label>
                    <div class="layui-input-block">
                        <input type="text" name="tableName" lay-verify="required" placeholder="请输入"
                               class="layui-input">
                    </div>
                </div>
            </div>
            <div class="layui-col-sm-3 layui-col-md3 layui-col-lg3">
                <div class="layui-form-item">
                    <label class="layui-form-label required">表注释</label>
                    <div class="layui-input-block">
                        <input type="text" name="tableComment" lay-verify="required" placeholder="请输入"
                               class="layui-input">
                    </div>
                </div>
            </div>
            <div class="layui-col-sm-3 layui-col-md3 layui-col-lg3">
                <div class="layui-form-item">
                    <label class="layui-form-label required">继承基类</label>
                    <div class="layui-input-block">
                        <input type="checkbox" name="extendBaseEntity" value="true" lay-skin="switch"
                               title="是|否">
                    </div>
                </div>
            </div>
        </div>
        <div class="layui-form-item">
            <table class="layui-hide" id="columnTable" lay-filter="columnTable"></table>
        </div>
        <div class="layui-form-item">
            <div class="layui-input-block">
                <button class="layui-btn" lay-submit lay-filter="submitForm">导入</button>
                <button type="reset" class="layui-btn layui-btn-primary" id="cancelForm">取消</button>
            </div>
        </div>
    </form>
</div>
</body>
<script type="text/html" id="columnTypeSelect" th:inline="javascript">
    <select name="columnType" lay-filter="columnType" lay-append-to="body">
        <option value="">请选择</option>
        <option value="char" {{ d.columnType=='char' ? 'selected' : '' }}>char</option>
        <option value="varchar" {{ d.columnType=='varchar' ? 'selected' : '' }}>varchar</option>
        <option value="text" {{ d.columnType=='text' ? 'selected' : '' }}>text</option>
        <option value="blob" {{ d.columnType=='blob' ? 'selected' : '' }}>blob</option>
        <option value="tinyint" {{ d.columnType=='tinyint' ? 'selected' : '' }}>tinyint</option>
        <option value="tinyint(1)" {{ d.columnType=='tinyint(1)' ? 'selected' : '' }}>tinyint(1)</option>
        <option value="boolean" {{ d.columnType=='boolean' ? 'selected' : '' }}>boolean</option>
        <option value="smallint" {{ d.columnType=='smallint' ? 'selected' : '' }}>smallint</option>
        <option value="mediumint" {{ d.columnType=='mediumint' ? 'selected' : '' }}>mediumint</option>
        <option value="int" {{ d.columnType=='int' ? 'selected' : '' }}>int</option>
        <option value="bigint" {{ d.columnType=='bigint' ? 'selected' : '' }}>bigint</option>
        <option value="float" {{ d.columnType=='float' ? 'selected' : '' }}>float</option>
        <option value="double" {{ d.columnType=='double' ? 'selected' : '' }}>double</option>
        <option value="decimal" {{ d.columnType=='decimal' ? 'selected' : '' }}>decimal</option>
        <option value="date" {{ d.columnType=='date' ? 'selected' : '' }}>date</option>
        <option value="datetime" {{ d.columnType=='datetime' ? 'selected' : '' }}>datetime</option>
        <option value="timestamp" {{ d.columnType=='timestamp' ? 'selected' : '' }}>timestamp</option>
        <option value="time" {{ d.columnType=='time' ? 'selected' : '' }}>time</option>
        <option value="year" {{ d.columnType=='year' ? 'selected' : '' }}>year</option>
    </select>
</script>

<script th:src="@{/static/js/jquery.js}"></script>
<script th:src="@{/static/layui/layui.js}"></script>
<script type="text/javascript" th:inline="javascript">
    layui.use(['element', 'layer', 'table', 'form'], function () {
        var element = layui.element;
        var layer = layui.layer;
        var table = layui.table;
        var form = layui.form;

        // table渲染
        var tableObj = table.render({
            elem: '#columnTable',
            cols: [
                [
                    {
                        fixed: 'left',
                        title: '<button id="addRow" class="layui-btn layui-btn-sm"><i class="layui-icon layui-icon-add-1"></i></button>',
                        width: 80,
                        templet: '<a class="layui-btn layui-btn-danger layui-btn-sm" lay-event="deleteRow"><i class="layui-icon layui-icon-delete"></i></a>'
                    },
                    {type: 'numbers', title: '序号', align: 'center'},
                    {field: 'columnComment', title: '字段注释', width: 200, align: 'center', edit: 'text'},
                    {field: 'columnName', title: '字段名', width: 200, align: 'center', edit: 'text'},
                    {field: 'columnType', title: '字段类型', width: 200, align: 'center', templet: '#columnTypeSelect'},
                    {field: 'dataLength', title: '字段长度', width: 150, align: 'center', edit: 'text'},
                    {field: 'decimalLength', title: '字段小数位', width: 150, align: 'center', edit: 'text'},
                    {field: 'defaultValue', title: '默认值', width: 150, align: 'center', edit: 'text'},
                    {
                        field: 'columnPk', title: '主键', width: 100, align: 'center',
                        templet: function (row) {
                            return '<input type="checkbox" name="columnPk" lay-filter="columnPk" value="true" ' + (row.columnPk === true ? 'checked' : '') + '>';
                        }
                    },
                    {
                        field: 'columnNotNull', title: '非空', width: 100, align: 'center',
                        templet: function (row) {
                            return '<input type="checkbox" name="columnNotNull" lay-filter="columnNotNull" value="true" ' + (row.columnNotNull === true ? 'checked' : '') + '>';
                        }
                    },
                    {
                        field: 'columnIncrement', title: '自增', width: 100, align: 'center',
                        templet: function (row) {
                            return '<input type="checkbox" name="columnIncrement" lay-filter="columnIncrement" value="true" ' + (row.columnIncrement === true ? 'checked' : '') + '>';
                        }
                    },
                ]
            ],
            data: [{
                columnComment: '',
                columnName: '',
                columnType: 'varchar',
                dataLength: 255,
                decimalLength: null,
                defaultValue: '',
                columnPk: false,
                columnNotNull: false,
                columnIncrement: false,
            }],
            page: false,
            skin: 'grid', // 边框风格
            size: 'lg', // 尺寸
            even: true, // 隔行背景
            done: function (res, curr, count) {
                var options = this;

                // 获取当前行数据
                table.getRowData = function (tableId, elem) {
                    var index = $(elem).closest('tr').data('index');
                    return table.cache[tableId][index] || {};
                };

                // 选择框事件
                form.on('select(columnType)', function (obj) {
                    var value = obj.value;
                    var data = table.getRowData(options.id, obj.elem);
                    data.columnType = value;
                });
                form.on('checkbox(columnPk)', function (obj) {
                    var elem = obj.elem;
                    var data = table.getRowData(options.id, elem);
                    data.columnPk = elem.checked;
                });
                form.on('checkbox(columnNotNull)', function (obj) {
                    var elem = obj.elem;
                    var data = table.getRowData(options.id, elem);
                    data.columnNotNull = elem.checked;
                });
                form.on('checkbox(columnIncrement)', function (obj) {
                    var elem = obj.elem;
                    var data = table.getRowData(options.id, elem);
                    data.columnIncrement = elem.checked;
                });

                // 单元格普通编辑事件
                table.on('edit(columnTable)', function (obj) {
                    var update = {};
                    update[obj.field] = obj.value;
                    obj.update(update);
                });
            }
        });

        // 监听按钮点击
        $(document).on('click', '#addRow', function(){
            var tableData = table.getData('columnTable');
            var row = {
                columnComment: '',
                columnName: '',
                columnType: 'varchar',
                dataLength: 255,
                decimalLength: null,
                defaultValue: '',
                columnPk: false,
                columnNotNull: false,
                columnIncrement: false,
            };
            tableData.push(row);
            table.reload('columnTable', {
                data: tableData,
            });
        });

        // 监听表格行工具
        table.on('tool(columnTable)', function (obj) {
            if (obj.event === 'deleteRow') {
                obj.del();
            }
        });

        // 保存
        form.on('submit(submitForm)', function (form) {
            var formData = form.field;
            var tableData = table.getData('columnTable');
            var data = {
                tableName: formData.tableName,
                tableComment: formData.tableComment,
                extendBaseEntity: formData.extendBaseEntity,
                columnList: tableData
            };
            $.ajax({
                url: '/genTable/add',
                type: 'POST',
                contentType: 'application/json',
                dataType: 'json',
                data: JSON.stringify(data),
                success: function (response) {
                    if (response.code === 200) {
                        // 关闭
                        parent.layer.close(parent.layer.getFrameIndex(window.name));
                        // 刷新
                        parent.location.reload();

                        layer.msg('新建成功', {icon: 1});
                    } else {
                        layer.msg(response.msg, {icon: 0});
                    }
                },
                error: function (xhr, status, error) {
                    layer.msg('请求失败', {icon: 0});
                }
            });
            return false;
        });

        // 取消
        $('#cancelForm').on('click', function () {
            parent.layer.close(parent.layer.getFrameIndex(window.name));
            return false;
        });

    });
</script>
</html>